import streamlit as st
import zipfile
import json
import io
from backend.streamlitsettings  import _max_width_
_max_width_()

st.title('Hap File View')


fileuploade = st.file_uploader('上传文件', accept_multiple_files=False)
if fileuploade != None:
    data = io.BytesIO(fileuploade.getvalue())

    hapfile = zipfile.ZipFile(data)
    filelist = hapfile.filelist

    with st.expander('文件列表'):
        st.table(filelist)

    cols = st.columns([0.1, 0.9])
    with cols[0]:
        st.image(hapfile.open('resources/base/media/icon.png').read(), width=48)

    # st.json(hapfile.open('pack.info').read().decode())
    with cols[1]:
        from backend.hap import PackInfo

        st.table({
            '名称': PackInfo(hapfile).bundleName(),
            '版本': PackInfo(hapfile).version(),
        })

    with st.expander('文件预览'):
        filename = st.selectbox('选择文件', [file.filename for file in hapfile.filelist])
        if filename.endswith('json'):
            st.json(hapfile.open(filename).read().decode())
        if filename.endswith('png'):
            st.image(hapfile.open(filename).read())